package com.ymatou.query.seller.trading.api.parameter.sp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.DataUtil;

@SuppressWarnings("rawtypes")
public class Sp_GetOrderListByOrderIdList {
	private Integer sellerId;
	private List<Integer> orderIds;

	public void setSellerId(Integer sellerId) {
		this.sellerId = sellerId;
	}

	public void setOrderIds(List<Integer> orderIds) {
		this.orderIds = orderIds;
	}

	public void execute() {
		if (null == orderIds || orderIds.size() == 0) {
			return;
		}
		DefaultWapper defaultWapper = new DefaultWapper();
		ymt_orders = defaultWapper
				.executeSql("select * from ymt_orders(nolock) where iBuyerId = "
						+ sellerId
						+ " and iOrderId in "
						+ DataUtil.toInOfSql(orderIds)
						+ " order by iOrderId asc");
		int rowCount = ymt_orders.size();
		if (rowCount > 0) {
			ymt_orderext = defaultWapper
					.executeSql("select iOrderId,iOrderType,sOrderSource,[bIsNeedUploadIdCard], [bHaveUploadedIdCard] from Ymt_OrderExt(nolock) where iOrderId in "
							+ DataUtil.toInOfSql(orderIds));
			ymt_o_ordernote = defaultWapper
					.executeSql("select iOrderId,sContent,[iRemarkLevel] from Ymt_O_OrderNote(nolock) where iorderid in "
							+ DataUtil.toInOfSql(orderIds));
			ymt_orderstate = defaultWapper
					.executeSql("select * from Ymt_OrderState(nolock) where iorderid in"
							+ DataUtil.toInOfSql(orderIds));
			ymt_orderpostpay = defaultWapper
					.executeSql("select iOrderId,fAmount,fUseGiftAmount from Ymt_OrderPostPay(nolock) where  iorderid in"
							+ DataUtil.toInOfSql(orderIds));
			ymt_orderinfo = defaultWapper
					.executeSql("select"
							+ " i.[iOrderId] , i.bPspProduct, i.sOrderInfoId , i.fOriginalPrice , i.iPriceType , i.iAmount , i.iBondedArea , i.iCatalogStatus , i.iCatalogType , i.iProductSubCategoryId"
							+ " , i.iSailProtected , i.iType , i.sCatalogId , i.sDescription , i.sPictureUrl , i.sProductId , i.sPropertyInfo , i.sReferenceUrl , i.sSKU , i.sTitle"
							+ " , e.sOrderInfoId as sOrderInfoExtId , e.bGiftAvail4Reward , e.iActivityId , e.iActivityTemplateId , i.iSalesType"
							+ " , i.fProductPrice , i.fProductOriginalPrice , i.fSellerCouponAmount , i.fYmtCouponAmount , i.fDiscount , i.fFreight"
							+ " , i.[fSellerPromotionAmount] , s.[PromotionId] , s.[PromotionType] , s.[PromotionName] , s.[MatchCondition] , s.[PromotionValue]"
							+ " , s.ReducedAmount , i.bSupportRtnWithoutReason, i.fThirdPartyDiscount,i.bPreSale,i.iProductRefundChannel"
							+ " from [dbo].[Ymt_OrderInfo] as i with(nolock)"
							+ " left join [dbo].[Ymt_OrderInfoExt] as e with(nolock) on i.[sOrderInfoId] = e.[sOrderInfoId]"
							+ " left join [dbo].[Ymt_SellerPromotion] as s with(nolock) on i.[iOrderId] = s.[OrderId] and i.[sOrderInfoId] = s.[OrderInfoId]"
							+ " where i.[iOrderId] in "
							+ DataUtil.toInOfSql(orderIds));
			ymt_ordersummary = defaultWapper
					.executeSql("select iOrderId, iBillType, sSummary from Ymt_OrderSummary(nolock) where  iorderid in"
							+ DataUtil.toInOfSql(orderIds));
			ymt_order_frozen = defaultWapper
					.executeSql("select * from Ymt_Order_Frozen(nolock) where  iorderid in"
							+ DataUtil.toInOfSql(orderIds));
			// ymt_ordertobill = defaultWapper
			// .executeSql("select iOrderid, sBillId from Ymt_OrderToBill(nolock) where  iorderid in"
			// + DataUtil.toInOfSql(orderIds)
			// + " and iaction >= 0");
			// Ymt_OrderToBill表作废
			ymt_ordertobill = defaultWapper
					.executeSql("select iOrderid, sBillId from Ymt_OrderToBill(nolock) where 1=0");
			// ymt_bill = defaultWapper
			// .executeSql("select a.* from Ymt_Bill(nolock) a, Ymt_OrderToBill(nolock) b where  b.iorderid in"
			// + DataUtil.toInOfSql(orderIds)
			// +
			// " and a.iaction >= 0 and b.iaction >= 0 and a.sBillId=b.sbillid");
			// Ymt_Bill表作废
			ymt_bill = defaultWapper
					.executeSql(" select * from Ymt_Bill(nolock) where 1=0");
			List<Map> t = defaultWapper
					.executeSql("select iOrderId, iTradingId, dUpdateTime from Ymt_TradingItem(nolock) where iOrderId in "
							+ DataUtil.toInOfSql(orderIds));
			if (!DataUtil.isNullOrEmpty(t)) {
				for (Map item : t) {
					if (DataUtil.Integerr.GetValueOrDefault(item
							.get("iTradingStatus")) == 2) {
						item.put("paided", 1);
					} else {
						item.put("paided", 0);
					}
				}
				Collections
						.sort(t,
								(x, y) -> (int) y.get("paided")
										- (int) x.get("paided"));
				Collections.sort(
						t,
						(x, y) -> (int) x.get("iOrderId")
								- (int) y.get("iOrderId"));
				orderTradingId = t.get(0);
			}
		}
	}

	private List<Map> ymt_orders;
	private List<Map> ymt_orderext;
	/**
	 * 获取备注列表
	 */
	private List<Map> ymt_o_ordernote;
	/**
	 * 获取订单金额详情列表
	 */
	private List<Map> ymt_orderstate;
	/**
	 * 获取订单补款列表
	 */
	private List<Map> ymt_orderpostpay;
	/**
	 * 获取订单商品详情列表
	 */
	private List<Map> ymt_orderinfo;
	/**
	 * 获取订单物流信息
	 */
	private List<Map> ymt_ordersummary;
	/**
	 * 获取订单冻结信息
	 */
	private List<Map> ymt_order_frozen;
	/**
	 * 
	 */
	private List<Map> ymt_ordertobill;
	/**
	 * 获取订单账单信息
	 */
	private List<Map> ymt_bill;
	/**
	 * orderTradingId
	 */
	private Map orderTradingId = initValue();

	private Map initValue() {
		Map orderTradingId = new HashMap();
		orderTradingId.put("iOrderId", 0);
		orderTradingId.put("iTradingId", 0);
		return orderTradingId;
	}

	public List<Map> getYmt_orders() {
		return ymt_orders;
	}

	public List<Map> getYmt_orderext() {
		return ymt_orderext;
	}

	public List<Map> getYmt_o_ordernote() {
		return ymt_o_ordernote;
	}

	public List<Map> getYmt_orderstate() {
		return ymt_orderstate;
	}

	public List<Map> getYmt_orderpostpay() {
		return ymt_orderpostpay;
	}

	public List<Map> getYmt_orderinfo() {
		return ymt_orderinfo;
	}

	public List<Map> getYmt_ordersummary() {
		return ymt_ordersummary;
	}

	public List<Map> getYmt_order_frozen() {
		return ymt_order_frozen;
	}

	public List<Map> getYmt_ordertobill() {
		return ymt_ordertobill;
	}

	public List<Map> getYmt_bill() {
		return ymt_bill;
	}

	public Map getOrderTradingId() {
		return orderTradingId;
	}

	public static void main(String[] args) {
		Sp_GetOrderListByOrderIdList sp = new Sp_GetOrderListByOrderIdList();
		List<Integer> orderIds = new ArrayList<Integer>();
		orderIds.add(100010622);
		orderIds.add(100010527);
		sp.setOrderIds(orderIds);
		sp.setSellerId(3383);
		sp.execute();
		// System.out.println(sp.getYmt_order_frozen());
		// System.out.println(sp.getYmt_orderext());
		// System.out.println(sp.getYmt_orderinfo());
		// System.out.println(sp.getYmt_orderpostpay());
		// System.out.println(sp.getYmt_orders());
		// System.out.println(sp.getYmt_orderstate());
		// System.out.println(sp.getYmt_ordersummary());
		// System.out.println(sp.getYmt_ordertobill());
		System.out.println(sp.getOrderTradingId());
	}
}
